<%--
  Created by IntelliJ IDEA.
  User: apink
  Date: 2019/12/16
  Time: 22:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
    <title>用户管理</title>
    <script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
    <div style="text-align: center;">
        <h2>这是一个大主页</h2>
        <p>看到这个页面，恭喜你！登陆成功！</p>
        查看登陆用户信息：
        <button id="findUser">查看</button>
        <button id="userClear">清空</button>
        <button id="outLogin">退出</button>
    </br></br>
        <table style="margin: auto;">
            <tbody>
                <tr><td>用户id：</td><td id="userid"></td></tr>
                <tr><td>用户名：</td><td id="username"></td></tr>
                <tr><td>密码：</td><td id="password"></td></tr>
                <tr><td>sessionid：</td><td id="sessionid"></td></tr>
            </tbody>
        </table>
        <br>
        <button id="cookieValBtn">查看cookie</button><span id="cookieVal"></span>
    </div>
    <script>
        const _pathName = window.document.location.pathname;
        const _projectName = _pathName.substring(0,_pathName.substr(1).indexOf('/')+1);

        $("#findUser").click(findUserMsg);
        $("#userClear").click(userClear);
        $("#outLogin").click(outLogin);
        $("#cookieValBtn").click(showCookie);

        function findUserMsg() {
            $.ajax({
                method: "post",
                url: _projectName + "/index/getUser",
                dataType: "json",
                success:function(result){
                    userClear();
                    if (result.code == 200) {
                        userShow(result.data);
                        localStorage.setItem("token", result.data);
                    }else {
                        alert(result.message);
                    }
                }});
        }
        function userShow(user) {
            $("#userid").html(user.userid);
            $("#username").html(user.username);
            $("#password").html(user.password);
            $("#sessionid").html(user.token);
        }
        function userClear() {
            $("#userid").html("");
            $("#username").html("");
            $("#password").html("");
            $("#sessionid").html("");
        }
        function outLogin() {
            window.location.href = _projectName + "/login/outLogin";
        }

        function showCookie() {
            var cookieVal = document.cookie;
            $("#cookieVal").html(cookieVal);
        }
    </script>
</body>
</html>
